package com.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project.bean.RoleBean;
import org.apache.ibatis.annotations.*;

import java.util.Set;

@Mapper
public interface IRoleMapper extends BaseMapper<RoleBean> {
    void addGrades(@Param("roleId") Integer roleId,
                   @Param("gradeIds") Integer[] gradeIds);

    @Delete("DELETE FROM t_role_grade WHERE fk_roleId = #{id};" +
            "DELETE FROM t_user_role WHERE fk_roleId = #{id};")
    void delRole(Integer id);

    void updateGrades(@Param("roleId") Integer roleId,
                      @Param("gradeId") Integer[] gradeId);

    @Select("SELECT DISTINCT ur.fk_userId,r.*  FROM t_user_role ur," +
            "t_role_grade rg,t_role r,t_grade g WHERE ur.fk_roleId = r.pk_roleId " +
            "AND ur.fk_roleId = rg.fk_roleId AND ur.fk_userId = #{userId}")
    @ResultMap("roleMapper")
    Set<RoleBean> findRoles(Integer userId);

    Set<RoleBean> findByUserId(Integer userId);
}
